<?php

	include_once('../config.php'); 
	session_start();
	LinkDataBase();
			
	$Array_DBTableName = DBTableNameGet($_SESSION['ID'], $_POST['year'], $_POST['season'], $_POST['tablename']);
	$filename = $Array_DBTableName[0];
	
	//Find question.
	$sql = "select * from ".$filename." LEFT JOIN sheet ON ".$filename.".table_name = binary(sheet.table_name) 
			WHERE table_number='".$_POST['tablename']."' AND 
			item NOT LIKE '%Base: All Respondents%' AND 
			item NOT LIKE '%Base: Respondents%'";
	
	//Only need Net values.
	if (isset($subchild['keyword']))
		$sql .= " AND item LIKE '%".$subchild['keyword']."%'";
		
	$result= mysql_query($sql);
	
	if(!$result)
		return;
	
	$numResult = mysql_num_rows($result);
			
	if ($numResult > 0) {
		?>
		<script type="text/javascript">
		var data = new google.visualization.DataTable();
		var myrow = new Array('');
						
            data.addColumn('string', 'string');
		<?php
		$item = "";
		while($row = mysql_fetch_assoc($result)) {
			if ((($item!="")&&($row['item']!='Sigma')&&
			(strpos($row['total'],'%'))&&
			($_POST['keyword']==''))||
			//For keyword exists.
			(($item!="")&&($row['item']!='Sigma')&&
			(strpos($row['total'],'%'))&&
			($_POST['keyword']!='')&&
			//contain is YES.
			(((strpos($item,(string)$_POST['keyword']))&&
			($_POST['contain']=='YES'))||
			//contain is NO.
			((!strpos($item,(string)$_POST['keyword']))&&
			($_POST['contain']=='NO'))))) {
		?>
				data.addColumn('number', '<?php echo str_replace("'","\'",str_replace('(Net)','',$item)); ?>');
        		data.addColumn({type:'string', role:'tooltip'}); //Tooltip with percentages
								
				var value = <?php echo str_replace('%','',$row['total'])/100; ?>;
				myrow.push(value, Math.round(value*100)+'%');			
		<?php
			}
			else if ($row['item']=='Sigma')
				break;
							
			$item = $row['item'];
		}
		?>
						
        // A column for custom tooltip content
		data.addRows([myrow]);
        
		var options = {
			legend: {position: 'right', textStyle: {color: 'blue', fontSize: 10}},
			vAxis: {textPosition:'in', maxValue:1, minValue:0, format:'#%'},
			chartArea: {left:0,width:"60%",height:"100%"},
			isStacked: true
			
			};
			
		var chart = new google.visualization.ColumnChart(document.getElementById('<?php echo $_POST['season'].$_POST['tablename']; ?>'));
		chart.draw(data, options);
        
		</script>
<?php
	}
?>
<div id="<?php echo $_POST['season'].$_POST['tablename']; ?>" style="width: 100%;"></div>